package com.coderknock.bbs.web.controller

import com.coderknock.bbs.mybatis.domain.User
import com.coderknock.bbs.service.UserService
import com.coderknock.bbs.web.response.Response
import org.springframework.boot.autoconfigure.mail.MailProperties
import org.springframework.dao.DataIntegrityViolationException
import org.springframework.data.redis.core.StringRedisTemplate
import org.springframework.mail.MailSender
import org.springframework.mail.SimpleMailMessage
import org.springframework.stereotype.Controller
import org.springframework.util.StringUtils
import org.springframework.web.bind.annotation.*
import java.util.*
import java.util.concurrent.TimeUnit
import javax.servlet.http.HttpSession

/**
 * <p></p>
 * @author 三产
 * @version 1.0
 * @date 2017-11-17
 * @QQGroup 213732117
 * @website http://www.coderknock.com
 * @copyright Copyright 2017 拿客 coderknock.com  All rights reserved.
 * @since JDK 1.8
 */
@Controller
@RequestMapping("/register")
class RegisterController(var userService: UserService, var mailSender: MailSender, var mailProperties: MailProperties, val template: StringRedisTemplate) {

    @PostMapping("/save")
    fun register(user: User, @RequestParam("verificationCode") verificationCode: String, session: HttpSession): String {
        var userFlag = userService.findUserByEmail(user.email!!)
        var msg: String
        if (userFlag != null) {
            msg = "该邮箱已被使用"
        } else {
            var vcKey = "verification-email-${user.email}"
            var ops = template.boundValueOps(vcKey)
            var redisVerificationCode = ops.get()
            if (!StringUtils.isEmpty(redisVerificationCode)) {
                if (verificationCode == redisVerificationCode) {
                    msg = try {
                        if (!StringUtils.isEmpty(user.passWord)) {
                            userService.save(user)
                            //注册之后要删除原来的验证码
                            template.delete(vcKey)
                            return "redirect:/login"
                        } else {
                            "密码不能为空"
                        }
                    } catch (e: DataIntegrityViolationException) {
                        "该邮箱已被使用"
                    }
                } else {
                    msg = "验证码填写错误"
                }
            } else {
                msg = "验证码已过期或验证码与邮箱不匹配"
            }
        }
        session.setAttribute("user", user)
        session.setAttribute("verificationCode", verificationCode)
        session.setAttribute("msg", msg)
        return "redirect:/register"
    }

    @ResponseBody
    @GetMapping("/sendMail")
    fun sendMail(@RequestParam("email") mail: String): Response<String> {
        var msg = SimpleMailMessage()
        msg.from = mailProperties.username
        msg.subject = "注册邮箱验证码"
        msg.setTo(mail)
        var verificationCode = UUID.randomUUID().toString().replace("-", "");
        msg.text = "您的验证码是 " + verificationCode
        var ops = template.boundValueOps("verification-email-$mail")
        //设置验证码 5 分钟失效
        ops.set(verificationCode, 5, TimeUnit.MINUTES)
        mailSender.send(msg)
        return Response(code = 1, msg = "邮件发送成功", data = "")
    }
}